const express = require('express');
const path = require('path');
const { initKnex } = require('./db/knex');
const bodyParser = require('body-parser');
const apiRouter = require('./routes/index');

initKnex();

const app = express();

app.use(bodyParser.json());
app.use(express.urlencoded({ extended: false }));

// 设置视图引擎为EJS
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

// 静态文件目录
app.use(express.static(path.join(__dirname, 'public')));

app.use('/api', apiRouter);

// 首页路由
app.get('/', (req, res) => {
  res.render('index', { title: '源码共享平台' });
});

// 启动端口
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`服务器启动，访问 http://localhost:${PORT}`);
});
